﻿
/* 
 * Since 2008 - 2012
 */
package com.hh.bss.project.controller;

import static com.hh.bss.util.extjs.JsonHelper.outJson;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;


import com.hh.bss.auth.client.OnlineManager;
import com.hh.bss.auth.client.OnlineUser;
import com.hh.bss.base.*;
import com.hh.bss.common.cache.DynaBusinessCache;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.project.dao.*;
import com.hh.bss.project.entity.*;
import com.hh.bss.project.service.*;
import com.hh.bss.project.vo.query.*;
import com.hh.bss.report.vo.query.ReportQuery;
import com.hh.bss.util.*;
import com.hh.bss.util.extjs.ExtJsPageHelper;
import com.hh.bss.util.extjs.ListRange;

import java.util.*;





@Controller
public class ContractJobCvOfferController extends BaseSpringController{
	//默认多列排序,example: username desc,createTime asc
	
	private static final String CONTENT_EXCEL_TYPE = "application/vnd.ms-excel";
	
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	private ContractJobCvOfferService contractJobCvOfferService;
	
	private ContractJobCvOffer contractJobCvOffer;
	java.lang.Integer id = null;
	private String[] items;

	
	
	/** 通过spring自动注入 */
	public void setContractJobCvOfferServiceImpl(ContractJobCvOfferService service) {
		this.contractJobCvOfferService = service;
	}	
	
	
	
	public void setId(java.lang.Integer val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}



	/**
	 * ExtGrid使用
	 * 列表
	 * @throws IOException
	 */
	public void list(HttpServletRequest request,HttpServletResponse response,ContractJobCvOfferQuery contractJobCvOfferQuery) throws IOException
	{
		OnlineManager onlineManager=super.getOnlineManager(request, response);
		if (onlineManager!=null){
			OnlineUser onlineUser=onlineManager.getOnlineUser();
			contractJobCvOfferQuery.setOnlineUser(onlineUser);
		}
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		Page page = contractJobCvOfferService.findPage(contractJobCvOfferQuery);
		
		List<ContractJobCvOffer> ContractJobCvOfferlist = (List) page.getResult();
		ListRange<ContractJobCvOffer> resultList = new ListRange<ContractJobCvOffer>();
		resultList.setRows(ContractJobCvOfferlist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response,resultList);
	}
    public String managerPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		return "forward:/jsp/jobCvOfferManagerPage.jsp";
		
	}
    public String jobOfferPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
    	return "forward:/jsp/project/contractJobCvOffer/contractJobCvOfferPage.jsp";
		
	}
	public void get(HttpServletRequest request,HttpServletResponse response,ContractJobCvOffer contractJobCvOffer) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			ContractJobCvOffer oldContractJobCvOffer = (ContractJobCvOffer)contractJobCvOfferService.getById(contractJobCvOffer.getId());
                         
			if(oldContractJobCvOffer!=null) result.put("data", oldContractJobCvOffer);
			result.put("success", true);
			result.put("msg", "成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
	public void jobofferlist(HttpServletRequest request,HttpServletResponse response,ContractJobCvOfferQuery contractJobCvOfferQuery) throws IOException
	{
		OnlineManager onlineManager=super.getOnlineManager(request, response);
		if (onlineManager!=null){
			OnlineUser onlineUser=onlineManager.getOnlineUser();
			contractJobCvOfferQuery.setOnlineUser(onlineUser);
		}
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		
		
		List<ContractJobCvOffer> ContractJobCvOfferlist = contractJobCvOfferService.findJobCvOffer(contractJobCvOfferQuery);
		ListRange<ContractJobCvOffer> resultList = new ListRange<ContractJobCvOffer>();
		resultList.setRows(ContractJobCvOfferlist);
		resultList.setTotal(ContractJobCvOfferlist.size());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response,resultList);
	}
	public void jobcvofferlist(HttpServletRequest request,HttpServletResponse response,ContractJobCvOfferQuery contractJobCvOfferQuery) throws IOException
	{
		OnlineManager onlineManager=super.getOnlineManager(request, response);
		if (onlineManager!=null){
			OnlineUser onlineUser=onlineManager.getOnlineUser();
			contractJobCvOfferQuery.setOnlineUser(onlineUser);
		}
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		Page page = contractJobCvOfferService.findJobCvOfferPage(contractJobCvOfferQuery);
		
		List<ContractJobCvOffer> ContractJobCvOfferlist = (List) page.getResult();
		ListRange<ContractJobCvOffer> resultList = new ListRange<ContractJobCvOffer>();
		resultList.setRows(ContractJobCvOfferlist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response,resultList);
	}
	public void cvofferlist(HttpServletRequest request,HttpServletResponse response,ContractJobCvOfferQuery contractJobCvOfferQuery) throws IOException
	{
		
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		OnlineManager onlineManager=super.getOnlineManager(request, response);
		if (onlineManager!=null){
			OnlineUser onlineUser=onlineManager.getOnlineUser();
			contractJobCvOfferQuery.setOnlineUser(onlineUser);
		}
		Page page = contractJobCvOfferService.findCvOfferPage(contractJobCvOfferQuery);
		
		List<ContractJobCvOffer> ContractJobCvOfferlist = (List) page.getResult();
		ListRange<ContractJobCvOffer> resultList = new ListRange<ContractJobCvOffer>();
		resultList.setRows(ContractJobCvOfferlist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response,resultList);
	}
	/**
	 * extGrid保存
	 * @throws IOException
	 */
	public void save(HttpServletRequest request,HttpServletResponse response,ContractJobCvOffer contractJobCvOffer) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        //设置创建用户和创建时间
			com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(ContractJobCvOffer.class,"creator")) {
						try{com.hh.bss.util.BeanUtils.setProperty(contractJobCvOffer,"creator",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			MultipartHttpServletRequest multipartRequest=null;
			MultipartFile multipartFile =null;
			
		       try{
		    	   multipartRequest = (MultipartHttpServletRequest) request;
		    	   multipartFile = multipartRequest.getFile("uploadFile");		    	  
		       }catch(Exception ex){ex.printStackTrace();}
		        
		       
		       if (multipartRequest!=null&&multipartFile!=null&&!multipartFile.isEmpty()){		    	   //
		    	   contractJobCvOfferService.saveFile(multipartRequest, multipartFile, contractJobCvOffer);		    	   
		       }
		       
			if (com.hh.bss.util.BeanUtils.isExistsProperty(ContractJobCvOffer.class,"createDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(contractJobCvOffer,"createDt",new Date());}catch(Exception ex){}
				}

			String msg="添加成功!";
		/*	List l=contractJobCvOfferService.getEntityDao().getHibernateTemplate().find("from ContractJobCvOffer t where t.recomId=?",contractJobCvOffer.getRecomId());
			if(l!=null&&l.size()>0) {
				ContractJobCvOffer oldContractJobCvOffer=(ContractJobCvOffer)l.get(0);
				if (oldContractJobCvOffer.getStatus()==1)
			         msg="人选已经上班，添加成功!";
			}*/
			contractJobCvOfferService.save(contractJobCvOffer);
			//新增Offer的时候 把职位联系人类别 默认为业绩分配人 类别
			//1.通过职位Id 找到职位  职位合作人 --->合作顾问 
			//2.通过职位Id 找到职位  支持助理 --->业务助理 
			contractJobCvOfferService.saveToOfferRole(contractJobCvOffer);
			
			//ContractJob  contractJob = contractJobService.getById()
			//contractJobOfferPartnerService.save(entity);
			
			//contractJobCvOfferService
			
			//DynaBusinessCache.refresh();
			result.put("success", true);
			result.put("msg", msg);
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
	/**
	 * extGrid修改
	 * @throws IOException
	 */
	public void update(HttpServletRequest request,HttpServletResponse response,ContractJobCvOffer contractJobCvOffer) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        ContractJobCvOffer oldcontractJobCvOffer = (ContractJobCvOffer)contractJobCvOfferService.getById(contractJobCvOffer.getId());
                         
			bind(request,oldcontractJobCvOffer);
                        //设置修改用户和修改时间 
                        com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(ContractJobCvOffer.class,"modifier")) {
						try{com.hh.bss.util.BeanUtils.setProperty(oldcontractJobCvOffer,"modifier",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(ContractJobCvOffer.class,"modifiedDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(oldcontractJobCvOffer,"modifiedDt",new Date());}catch(Exception ex){}
				}

			MultipartHttpServletRequest multipartRequest=null;
			MultipartFile multipartFile =null;
			
		       try{
		    	   multipartRequest = (MultipartHttpServletRequest) request;
		    	   multipartFile = multipartRequest.getFile("uploadFile");		    	  
		       }catch(Exception ex){ex.printStackTrace();}
		        
		       
		       if (multipartRequest!=null&&multipartFile!=null&&!multipartFile.isEmpty()){		    	   //
		    	   contractJobCvOfferService.saveFile(multipartRequest, multipartFile, oldcontractJobCvOffer);		    	   
		       }

			contractJobCvOfferService.update(oldcontractJobCvOffer);
			DynaBusinessCache.refresh();
			result.put("success", true);
			result.put("msg", "修改成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
	/**
	 * extGrid删除
	 * @throws IOException
	 */
	public void delete(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		String ids = request.getParameter("ids");
		String[] idarray = ids.split(",");
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			 for(String id : idarray){
				 contractJobCvOfferService.removeById(Integer.parseInt(id));
			 }
			result.put("success", true);
			result.put("msg", "删除成功");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
	/**
	 * 导出记录
	 * @param request
	 * @param response
	 * @param reportQuery
	 * @throws IOException
	 */
	public void exportrecord(HttpServletRequest request,HttpServletResponse response,ContractJobCvOfferQuery contractJobCvOfferQuery) throws IOException
	{
		//
		Map<String, Object> result = new HashMap<String, Object>();
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		OnlineManager onlineManager=super.getOnlineManager(request, response);
		if (onlineManager!=null){
			OnlineUser onlineUser=onlineManager.getOnlineUser();
			if (onlineUser!=null)contractJobCvOfferQuery.setOnlineUser(onlineUser);
			//if (onlineUser!=null)reportQuery.setAccount(onlineUser.getUsername());
			
		}
		//Page page = accountReportService.workKpi(reportQuery);
		
		List list=contractJobCvOfferService.exportCvOfferList(contractJobCvOfferQuery);
		
		try{
       		String title=new String(("录用人选.xls").getBytes("GBK"),"iso8859-1"); 
       		//HttpServletResponse response=ServletActionContext.getResponse();
       		response.setHeader("Content-Disposition", "attachment;filename=\""+title+"\"");
       		response.setContentType(CONTENT_EXCEL_TYPE);
       		//response.setContentType("application/octet-stream;charset=UTF-8");
       		ServletOutputStream os = response.getOutputStream();
       		Map titles=new LinkedHashMap();
       		titles.put("custName", "客户公司");
       		titles.put("jobName", "职位名");
       		titles.put("cvName", "候选人名");
       		titles.put("mobile", "手机");
       		titles.put("email", "电邮");
       		titles.put("applyD", "录用日期");
       		
       		titles.put("ondutyD", "预估上班日期");
       		titles.put("checkinD", "实际上班日期");
       		
       		titles.put("status", "录用结果");
       		
       		
       		titles.put("isPn", "账单");
       		titles.put("estimateAr", "预计收费");
       		titles.put("finalAr", "实际收费");
       		titles.put("jobManager", "职位负责人");
       		
       		HSSFWorkbook wb = ExcelHelper.createExcelWorkbook("业绩检测", list, titles);
       		wb.write(os);
       		os.flush();
       		os.close(); 
       	}catch(Exception ex){
       		 ex.printStackTrace();
       	}
		//outJson(response, result);
	}
	
	
}
